#include <signal.h>
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
struct sigaction act;
static int count;
void catchctrl(int signo)
{
    count++;
    char msg[]="ctrl+c entered!\n";
    int msglen=sizeof msg;
    write(STDERR_FILENO,msg,msglen);
    if(count == 2)
    {
        char errmsg[]="Failed to restore SIGINT\n";
        int mesglen=sizeof msg;
        act.sa_handler=SIG_DFL;
        if(sigaction(SIGINT,&act,NULL)==-1)
            write(STDERR_FILENO,errmsg,mesglen);
    }
}

int main()
{
    act.sa_handler=catchctrl;
    act.sa_flags=0;
    if((sigemptyset(&act.sa_mask)==-1)||(sigaction(SIGINT,&act,NULL)==-1))
        perror("failed to set SIGINT to handle Ctrl-c");
    for(;;);
    return 0;
}